package org.an.proj.contacts.util;

import android.text.TextUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by an on 2016/11/5.<br/>
 * Scheme只是://之前的部分，并不包含://
 */

public class SchemeUtil {
  public static final String SCHEME = "contact";

  private static final String SUFFIX_SCHEME = "://";
  private static final String PARAMS = "?";

  public static String getScheme(String url) {
    int index = url.indexOf(SUFFIX_SCHEME);
    if (index != -1) {
      return url.substring(0, index);
    }
    return null;
  }

  public static String getAction(String url) {
    int indexScheme = url.indexOf(SUFFIX_SCHEME);
    int indexParams = url.indexOf(PARAMS);
    if (indexScheme != -1) {
      return url.substring(indexScheme + SUFFIX_SCHEME.length(),
          indexParams == -1 ? url.length() : indexParams);
    }
    return null;
  }

  /**
   * 解析出url参数中的键值对 如 "index.jsp?Action=del&id=123"，解析出Action:del,id:123存入map中
   * fixed by xiejiangquan:2016/4/22
   * 形如：bShareShow=0&htmlurlstring=http%3a%2f%2ftesth5.lianjia.com%2fapp%2flist-loupandt%3ftype%3dmsg
   * 应当先处理，再decode
   * 如果先解析得到 bShareShow=0&htmlurlstring=http://testh5.lianjia.com.flist-loupandt?type=dmsg
   * 处理的时候，split就会出错
   *
   * @param url url地址
   * @return url请求参数部分
   */
  public static Map<String, String> getParams(String url) {
    if (url == null) return null;
    int index = url.indexOf("?");
    if (index == -1) return null;
    String strUrlParam = url.substring(index, url.length());
    Map<String, String> mapRequest = new HashMap<String, String>();
    String[] arrSplit = strUrlParam.split("[&]");
    for (String strSplit : arrSplit) {
      String[] arrSplitEqual = null;
      arrSplitEqual = strSplit.split("[=]");
      // 解析出键值
      if (arrSplitEqual.length > 1) {
        String value = "";
        try {
          value = URLDecoder.decode(arrSplitEqual[1], "utf-8");
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
        // 正确解析
        mapRequest.put(arrSplitEqual[0], value);
      } else {
        if (arrSplitEqual[0] != "") {
          // 只有参数没有值，不加入
          mapRequest.put(arrSplitEqual[0], "");
        }
      }
    }
    return mapRequest;
  }

  /**
   * 去掉url中的路径，留下请求参数部分
   *
   * @param strURL url地址
   * @return url请求参数部分
   */
  private static String TruncateUrlPage(String strURL) {
    String strAllParam = null;
    String[] arrSplit = null;

    strURL = strURL.trim();

    arrSplit = strURL.split("[?]");
    if (strURL.length() > 1) {
      if (arrSplit.length > 1) {
        if (arrSplit[1] != null) {
          strAllParam = arrSplit[1];
        }
      }
    }

    return strAllParam;
  }

  /**
   * 解析出url参数中的键值对 如 "index.jsp?Action=del&id=123"，解析出Action:del,id:123存入map中
   * fixed by xiejiangquan:2016/4/22
   * 形如：bShareShow=0&htmlurlstring=http%3a%2f%2ftesth5.lianjia.com%2fapp%2flist-loupandt%3ftype%3dmsg
   * 应当先处理，再decode
   * 如果先解析得到 bShareShow=0&htmlurlstring=http://testh5.lianjia.com.flist-loupandt?type=dmsg
   * 处理的时候，split就会出错
   *
   * @param url url地址
   * @return url请求参数部分
   */
  public static Map<String, String> URLRequest(String url) {
    Map<String, String> mapRequest = new HashMap<String, String>();

    if (TextUtils.isEmpty(url)) return mapRequest;

    String strUrlParam = TruncateUrlPage(url);
    if (strUrlParam == null) {
      return mapRequest;
    }

    String[] arrSplit = null;
    arrSplit = strUrlParam.split("[&]");
    for (String strSplit : arrSplit) {
      String[] arrSplitEqual = null;
      arrSplitEqual = strSplit.split("[=]");
      // 解析出键值
      if (arrSplitEqual.length > 1) {
        String value = "";
        try {
          value = URLDecoder.decode(arrSplitEqual[1], "utf-8");
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
        // 正确解析
        mapRequest.put(arrSplitEqual[0], value);
      } else {
        if (arrSplitEqual[0] != "") {
          // 只有参数没有值，不加入
          mapRequest.put(arrSplitEqual[0], "");
        }
      }
    }
    return mapRequest;
  }
}
